package com.cabd.web.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cabd.domain.PredictRow;
import com.cabd.domain.User;
import com.cabd.service.ShowInfo;
import com.cabd.service.impl.PredictPage;
import com.cabd.service.impl.ShowInfoImpl;
import com.cabd.staticfinal.DownLoadAndUpLoadPath;

public class ShowInfoServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	PredictPage predictPage = new PredictPage();
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		predictPage.setOnePageNumbers(12);
		if(request.getParameter("folderName")!=null) {
			predictPage.setFolderName(request.getParameter("folderName"));
		}
		if(request.getParameter("fileName")!=null) {
			predictPage.setFileName(request.getParameter("fileName"));
		}
		System.out.println(predictPage.pagesNumber());
		String method = request.getParameter("method");
		if(method!=null&&method.equals("next")) {
			predictPage.next();
		}else if(method!=null&&method.equals("prior")){
			predictPage.prior();
		}else {
			predictPage.first();
			System.out.println("first");
		}
		ShowInfo showInfo = new ShowInfoImpl();
		if(predictPage.getFileName()==null) {
			response.sendRedirect(request.getContextPath() + "\\home");
			return;
		}
		String fileType = showInfo.getFileType(predictPage.getFileName());
		String path = DownLoadAndUpLoadPath.downLoadAndUpLoadPath+"\\"+predictPage.getFolderName()+"\\output\\"+predictPage.getFileName();
		System.out.println(path);
		if(fileType.equals("csv")) {
			int begin = predictPage.getBegin();
			int end = predictPage.getEnd();
			System.out.println(begin);
			System.out.println(end);
			//E:\Python2018\workspace\HYZX1\Data\test_A\output\predict2.csv
			//
			ArrayList<PredictRow> arrayList = showInfo.showPredictCSV(path, begin, end);
			int pageNow = predictPage.getBegin()/predictPage.getOnePageNumbers() + 1;
			if(pageNow>1) {
				request.setAttribute("prior", true);
			}else if(pageNow == 1) {
				request.setAttribute("prior", false);
				pageNow = 1;
			}else {
				pageNow = 0;
				return;
			}
			if(pageNow<predictPage.pagesNumber()) {
				request.setAttribute("next", true);
			}else if(pageNow == predictPage.pagesNumber()) {
				request.setAttribute("next", false);
				System.out.println("next");
				pageNow = predictPage.pagesNumber();
			}else {
				return;
			}
			//将当前页数、总页数和读取到的预测内容发送到浏览器
			int allPages = predictPage.pagesNumber();
			System.out.println("共:    "+predictPage.pagesNumber());
			request.setAttribute("page", pageNow);
			request.setAttribute("allPages", allPages);
			request.setAttribute("showInfo", arrayList);
			request.getRequestDispatcher("/ShowPredict.jsp").forward(request, response);
			User user = (User)request.getSession().getAttribute("user");
			if(user!=null) {
				System.out.println(user.getId()+"查看了： "+predictPage.getFileName()+"第"+pageNow+"页");
			}else {
				System.out.println("游客查看了："+predictPage.getFileName() + "第" + pageNow + "页");
			}
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}